package org.web.standardplatform.client.helper.view.select;

import org.apache.commons.lang3.StringUtils;
import org.web.standardplatform.client.domain.attribute.AttributeDTO;
import org.web.standardplatform.client.domain.attribute.JavaAttributeDTO;
import org.web.standardplatform.client.helper.view.rules.TsObjectArrayBuilder;

import java.util.*;
import java.util.regex.*;

public class PatternValueLabelParser implements SelectTypeConstant {

    public static String parseAttributeForSelect(AttributeDTO attributeDTO) {
        JavaAttributeDTO javaAttributeDTO = attributeDTO.getJavaAttribute();
        String patternValue = null;
        if (javaAttributeDTO.getDtoRuleList() != null) {
            for (String rule : javaAttributeDTO.getDtoRuleList()) {
                if (rule.contains("Pattern")) {
                    patternValue = rule;
                    break;
                }
            }
        }
        String value = SelectTypeConstant.SELECT_OPERATION_EXTEND;
        if (StringUtils.isNotBlank(patternValue)) {
            List<ValueLabelObject> list = parse(patternValue);

            TsObjectArrayBuilder builder = TsObjectArrayBuilder.create();
            for (ValueLabelObject valueLabelObject : list) {
                builder.addItem()
                        .field("value", valueLabelObject.getValue())
                        .field("label", valueLabelObject.getLabel())
                        .end();
            }

            // 直接构建字段 options，不要再嵌套封装一次
            value = "{\n" + builder.buildAsField("options") + "\n}";
        }

        return value;
    }


    public static List<ValueLabelObject> parse(String annotationText) {
        String regexp = extractAttribute(annotationText, "regexp");
        String message = extractAttribute(annotationText, "message");
        return buildValueLabelList(regexp, message);
    }

    private static String extractAttribute(String text, String attr) {
        Pattern p = Pattern.compile(attr + "\\s*=\\s*\"([^\"]*)\"");
        Matcher m = p.matcher(text);
        return m.find() ? m.group(1) : null;
    }

    private static List<ValueLabelObject> buildValueLabelList(String regexp, String message) {
        List<ValueLabelObject> result = new ArrayList<>();

        if (message != null && message.contains(":")) {
            String[] pairs = message.split(";");
            for (String pair : pairs) {
                String[] parts = pair.split(":");
                if (parts.length == 2) {
                    String value = parts[0].trim();
                    String label = parts[1].trim();
                    result.add(new ValueLabelObject(value, label));
                }
            }
        } else if (regexp != null) {
            Pattern pattern = Pattern.compile("\\^?\\(?([a-zA-Z0-9|_\\-]+)\\)?\\$?");
            Matcher matcher = pattern.matcher(regexp);
            if (matcher.find()) {
                String[] values = matcher.group(1).split("\\|");
                for (String value : values) {
                    value = value.trim();
                    result.add(new ValueLabelObject(value, value));
                }
            }
        }

        return result;
    }
}
